package com.midongtech.news;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;


public class TitleBar extends LinearLayout {

    View mView;
    DebugACtion debugACtion;
    private TextView mTitleTv;
    private RelativeLayout rl_back;
    private int clickNum;
    private BackArrowView bv_close;
    private ImageView iv_tips2;
    private BackArrowView bv_back;
    private ImageView tv_feedback;

    public TitleBar(Context context) {
        super(context);
        init(context);
    }

    public TitleBar(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public TitleBar(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    private void init(final Context context) {
        View view = LayoutInflater.from(context).inflate(R.layout.mdtec_ui_title_bar, this, true);
        mView = view.findViewById(R.id.view);
        bv_close = view.findViewById(R.id.bv_close);
        iv_tips2 = view.findViewById(R.id.iv_tips2);
        tv_feedback = view.findViewById(R.id.tv_feedback);

        rl_back = mView.findViewById(R.id.rl_back);
        rl_back.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (context instanceof Activity) {
                    ((Activity) context).finish();
                }
            }
        });
        bv_back = mView.findViewById(R.id.bv_back);
        mTitleTv = mView.findViewById(R.id.tv_title);
        mTitleTv.setTextSize(TypedValue.COMPLEX_UNIT_SP, 18);


    }


    public void setTextColor(String color) {
        mTitleTv.setTextColor(Color.parseColor(color));
        bv_back.setmColor(Color.parseColor(color));
        bv_close.setmColor(Color.parseColor(color));
    }


    public void setCloseListener(OnClickListener listenr) {
        if (listenr == null) {
            bv_close.setVisibility(View.GONE);
        } else {
            bv_close.setVisibility(View.VISIBLE);
            bv_close.setOnClickListener(listenr);
        }
    }


    public void setTitleText(final String text) {
        if (text != null && mTitleTv != null) {
            mTitleTv.post(new Runnable() {
                @Override
                public void run() {
                    mTitleTv.setText(text);
                }
            });

        }
    }


    public interface DebugACtion {
        void onDebug();
    }
}
